package com.dnake.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.dnake.Constant;
import com.dnake.entity.EventSettingBean;
import com.dnake.misc.SysAccess;
import com.dnake.panel.R;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class LockTypeFragment extends SupportFragment {
    @Bind(R.id.rb_one)
    RadioButton rbOne;
    @Bind(R.id.rb_two)
    RadioButton rbTwo;
    @Bind(R.id.tv_delay)
    TextView tvDelay;
    @Bind(R.id.sb_delay)
    SeekBar sbDelay;
    @Bind(R.id.ll_electric_control)
    LinearLayout llElectricControl;
    private View mView;

    public static LockTypeFragment newInstance() {
        return new LockTypeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_lock_type, container, false);
        ButterKnife.bind(this, mView);
        initData();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    private void initData() {
        //延迟落锁时间
        sbDelay.setProgress(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME));
        tvDelay.setText(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)+"秒");
        //锁类型 true磁力锁 false电控锁
        if (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_TYPE)){
            rbOne.setChecked(true);
            llElectricControl.setVisibility(View.VISIBLE);
        }else {
            rbTwo.setChecked(true);
            llElectricControl.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_LOCK_TYPE) {
            if ("1".equals(eventBusBean.getName())) {
                if (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_TYPE)){
                    rbTwo.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_LOCK_TYPE,false);
                    llElectricControl.setVisibility(View.GONE);
                }else {
                    rbOne.setChecked(true);
                    SharedPreferencesUtils.put(Constant.SP_LOCK_TYPE,true);
                    llElectricControl.setVisibility(View.VISIBLE);
                }
            } else if ("up".equals(eventBusBean.getName())){
                if (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_TYPE)){
                    if (SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)<15){
                        SharedPreferencesUtils.put(Constant.SP_DELAY_TIME, SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)+1);
                        sbDelay.setProgress(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME));
                        tvDelay.setText(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)+"秒");
                        SysAccess.lock.timeout = SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME);
                        SysAccess.save();
                    }
                }
            }else if ("down".equals(eventBusBean.getName())){
                if (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_TYPE)){
                    if (SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)>1){
                        SharedPreferencesUtils.put(Constant.SP_DELAY_TIME, SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)-1);
                        sbDelay.setProgress(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME));
                        tvDelay.setText(SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)+"秒");
                        SysAccess.lock.timeout = SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME);
                        SysAccess.save();
                    }
                }
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
